<?php
namespace app\admin\controller\develop;
use app\admin\controller\Controller;
use app\common\model\divide\Receive as ReceiveModel;
use app\store\model\Express as ExpressModel;
use app\admin\model\order\Order as OrderModel;

use app\store\model\store\shop\Clerk as ShopClerkModel;
use org\RabbitMQ;
class Order extends Controller
{

    /* @var OrderModel $model */
    private $model;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new OrderModel;
    }

    /**
     * 订单导出
     * @param string $dataType
     * @throws \think\exception\DbException
     */
    public function export()
    {
        return $this->model->exportList($this->request->param());
    }



    public function index()
    {

        // 订单列表
        $model = new OrderModel;
        //$list = $model->getList($this->request->param());
        //return $this->fetch('index', compact( 'list'));
        return $this->fetch('index', [
            // 充值记录列表
            'list' => $list = $model->getList($this->request->param()),
            //支付类型
            'payList' => $model::getPaylist(),
            //付款状态
            'payStatusList'=>$model::getPayStatusList(),

        ]);

    }



    /**
     * 订单详情
     * @param $order_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function detail($order_id)
    {
        // 订单详情
        $detail = OrderModel::detail($order_id);
        // 物流公司列表
        $expressList = ExpressModel::getAll();
        // 门店店员列表
        $shopClerkList = (new ShopClerkModel)->getList(true);
        return $this->fetch('detail', compact(
            'detail',
            'expressList',
            'shopClerkList'
        ));
    }


    public function abonus($order_id){
        // 订单详情
        $orderNo=db('order')->where('order_id',$order_id)->value('order_no');
        $model = new ReceiveModel();
        $id=$model->where('order_no',$orderNo)->value('receives_id');
        $extend=db('divide_receives_user')->where('receives_id',$id)->value('extend');

        return $this->fetch('abonus',  [
            // 充值记录列表
            'data' =>$data=$model->getList($id),
            // 属性集
            'list' =>$list=json_decode($extend,true),

        ]);


    }

    public function accounts(){
        $params=$this->request->param();
        $detail = OrderModel::detail($params['id']);

        if($detail['pay_type']['value']==20 && $detail['pay_price']>=1){
            $rem=new RabbitMQ();
            $data=['orderId'=>$detail['order_id'],'time'=>time()];
            $rem->send($data,'usersListData','usersListDataLog','usersListDataLog');
            return $this->renderSuccess('分账成功');
        }else{
            return $this->renderError('支付方式不正确或者分账金额小于1元');
        }
    }

}